package com.baidu.videoseries.widget;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;

import com.baidu.videoseries.R;
import com.kvin.toolkit.utils.LogUtils;


/**
 * Created by leiyun on 2018/4/16.
 */
public class ControlView extends ViewGroup {

    private SeekBar mLightBar;
    private SeekBar mVoiceBar;
    private SeekBar mProgressBar;
    private ImageView mPlayIv;

    private int mLightMax;
    private int mLightProgress;
    private int mMax;
    private int mProgress;
    private int mVoiceMax;
    private int mVoiceProgress;

    private Rect mRect;


    private int paddingBorder = 10;
    private int paddingBottom = 30;
    private DisplayMetrics mMetrics;


    public ControlView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mMetrics = getResources().getDisplayMetrics();


    }

    public int getLightMax() {
        return mLightMax;
    }

    public void setLightMax(int mLightMax) {
        this.mLightMax = mLightMax;
    }

    public int getLightProgress() {
        return mLightProgress;
    }

    public void setLightProgress(int mLightProgress) {
        this.mLightProgress = mLightProgress;
    }

    public int getProgress() {
        return mProgress;
    }

    public void setProgress(int mProgress) {
        this.mProgress = mProgress;
    }

    public int getVoiceProgress() {
        return mVoiceProgress;
    }

    public void setVoiceProgress(int mVoiceProgress) {
        this.mVoiceProgress = mVoiceProgress;
    }

    public int getVoiceMax() {
        return mVoiceMax;
    }

    public void setVoiceMax(int mVoiceMax) {
        this.mVoiceMax = mVoiceMax;
    }

    public int getMax() {
        return mMax;
    }

    public void setMax(int mMax) {
        this.mMax = mMax;
    }

    /**
     *
     */
    private void initSeekBar() {
        int count = getChildCount();
        View v;
        if (count < 3) return;
        for (int i = 0; i < count; i++) {
            v = getChildAt(i);
//            String tag = v.getTag().toString();
            LogUtils.v("--------id="+v.getId());
//            if (v.getId()==R.id.light_bar) mLightBar = (SeekBar) v;
//            if (v.getId()==R.id.progress_bar) mProgressBar = (SeekBar) v;
//            if (v.getId()==R.id.voice_bar) mVoiceBar = (SeekBar) v;
        }
        mLightBar.setLayoutParams(getLp(mMetrics.heightPixels - 100));
        mLightBar.setMax(10);
//        mLightBar.setProgress(0);
        mLightBar.setFocusableInTouchMode(true);
        mLightBar.setThumb(getResources().getDrawable(R.mipmap.play_seek));

        mProgressBar.setLayoutParams(getLp(mMetrics.widthPixels - 100));
        mProgressBar.setMax(100);
//        mProgressBar.setProgress(0);
        mProgressBar.setFocusableInTouchMode(true);
        mProgressBar.setThumb(getResources().getDrawable(R.mipmap.play_seek));

        mVoiceBar.setLayoutParams(getLp(mMetrics.heightPixels - 100));
        mVoiceBar.setMax(10);
//        mVoiceBar.setProgress(0);
        mVoiceBar.setFocusableInTouchMode(true);
        mVoiceBar.setThumb(getResources().getDrawable(R.mipmap.play_seek));

        this.mPlayIv = new ImageView(getContext());
        this.mPlayIv.setImageResource(R.mipmap.icon_pop_play);
        addView(mPlayIv);


        mRect = new Rect();

    }

    /**
     * default lp
     */
    private LayoutParams getLp(int width) {
        LayoutParams lp = new LayoutParams(width - 2 * paddingBorder, LayoutParams.WRAP_CONTENT);
        return lp;
    }

    public ControlView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ControlView(Context context) {
        super(context);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (mLightBar == null || mProgressBar == null || mVoiceBar == null) return;
        mRect.left = (mMetrics.widthPixels - mLightBar.getMeasuredWidth()) / 2;
        mRect.top = mMetrics.heightPixels - paddingBorder - mLightBar.getMeasuredHeight();
        mRect.right = mRect.left + mLightBar.getMeasuredWidth();
        mRect.bottom = mRect.top + mLightBar.getMeasuredHeight();
        ViewHelper.Layout(mLightBar, mRect);
        mLightBar.setRotation(-90);
        mLightBar.setTranslationX(-mProgressBar.getMeasuredWidth() / 2 + paddingBorder * 5);
        mLightBar.setTranslationY(-mLightBar.getMeasuredWidth() / 2);

        mRect.left = paddingBorder;
        mRect.top = mMetrics.heightPixels - paddingBottom - mLightBar.getMeasuredHeight();
        mRect.right = mRect.left + mProgressBar.getMeasuredWidth();
        mRect.bottom = mRect.top + mLightBar.getMeasuredHeight();
        ViewHelper.Layout(mProgressBar, mRect);

        mRect.left = (mMetrics.widthPixels - mLightBar.getMeasuredWidth()) / 2;
        mRect.top = mMetrics.heightPixels - paddingBorder - mLightBar.getMeasuredHeight();
        mRect.right = mRect.left + mLightBar.getMeasuredWidth();
        mRect.bottom = mRect.top + mLightBar.getMeasuredHeight();
        ViewHelper.Layout(mVoiceBar, mRect);
        mVoiceBar.setRotation(-90);
        mVoiceBar.setTranslationX(mProgressBar.getMeasuredWidth() / 2 - paddingBorder * 5);
        mVoiceBar.setTranslationY(-mLightBar.getMeasuredWidth() / 2);

        //layout play view
        mRect.left = (mMetrics.widthPixels - mPlayIv.getMeasuredWidth()) / 2;
        mRect.top = (mMetrics.heightPixels - mPlayIv.getMeasuredHeight()) / 2;
        mRect.right = mRect.left + mPlayIv.getMeasuredWidth();
        mRect.bottom = mRect.top + mPlayIv.getMeasuredHeight();
        ViewHelper.Layout(mPlayIv, mRect);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        initSeekBar();
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
